package com.hunktimes.wholesale.ui.pages

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BasePageFragment
import com.hunktimes.wholesale.databinding.TitleBarListBinding
import com.hunktimes.wholesale.net.model.AddressBundle
import com.hunktimes.wholesale.util.Constants
import com.hunktimes.wholesale.util.L

/**
 * Author: Delete
 * Date: 2020/7/28 11:53
 * Description:
 *
 */
class AddressListFragment : BasePageFragment(R.layout.title_bar_list) {

    override val titleRes: Int
        get() = R.string.title_address_list
    lateinit var viewBinding: TitleBarListBinding
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding = TitleBarListBinding.bind(view)
    }

    override fun onStart() {
        super.onStart()
        initAddresses()
    }

    val addressAdapter = AddressesAdapter()
    fun initAddresses() {
        val addressLm = LinearLayoutManager(requireContext())
        addressLm.orientation = LinearLayoutManager.VERTICAL
        viewBinding.recyclerList.recyclerView.layoutManager = addressLm
        viewBinding.recyclerList.recyclerView.adapter = addressAdapter

        val data = mutableListOf<String>()
        for (i in 1..6) {
            data.add("AddressAapter item $i")
        }
        addressAdapter.data = data
        addressAdapter.setOnItemClickListener { adapter, view, position ->
            val address = adapter.getItem(position) as String
            val bundle = bundleOf(Constants.key_receive_address to address)
            L.d("AddressListFragment popup bundle -> $bundle")
            rootNavController.navigate(
                R.id.action_navigation_address_list_to_navigation_create_order,
                bundle
            )
        }
        addressAdapter.addChildClickViewIds(R.id.item_addr_edit_tv)
        addressAdapter.setOnItemChildClickListener { adapter, view, position ->
            val address = AddressBundle("Jax", "17071590613", "summoner rift")
            val bundle = bundleOf(Constants.key_address_bundle to address)
            if (view.id == R.id.item_addr_edit_tv) {
                rootNavController.navigate(
                    R.id.action_navigation_address_list_to_navigation_address_edit,
                    bundle
                )
            }
        }
    }

    /*
    Navigation.findNavController(it).navigate(R.id.action2third, null, new
    NavOptions.Builder().setPopUpTo(R.id.firstFragment, false).build())可以实现你的要求，
     */
}

class AddressesAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_address) {
    override fun convert(holder: BaseViewHolder, item: String) {

    }

}